//
//  FaceMonitorViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/25.
//

#import "FaceMonitorViewController.h"
#import "WearWatchViewController.h"
@interface FaceMonitorViewController ()

@end

@implementation FaceMonitorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHideNavigationBar = YES;
    self.view.backgroundColor = HexColor(@"#F8F8F8");
    
   NSString *path =  [[NSBundle mainBundle]pathForResource:@"faceresult" ofType:@"json"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    NSDictionary * jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *dataArr = SafeArray(jsonObj[@"data"]);
    
    NSMutableArray *titleArr = @[].mutableCopy;
    NSMutableArray *rightArr = @[].mutableCopy;
    NSMutableArray *detailArr = @[].mutableCopy;
    NSMutableArray *imageArr = @[].mutableCopy;
    for (int i = 0; i < dataArr.count; i++) {
        NSDictionary *itemDic = SafeDictionary(dataArr[i]);
        NSString *text = itemDic[@"text"];
        if(i == 0){
            NSArray *arr = [text componentsSeparatedByString:@"\n"];
            NSArray *secArr = [arr.firstObject componentsSeparatedByString:@"："];
            [titleArr addObject:secArr.firstObject];
            [rightArr addObject:secArr.lastObject];
            [detailArr addObject:arr.lastObject];
        }else if (i == 1){
            NSArray *arr = [text componentsSeparatedByString:@"："];
            [titleArr addObject:arr.firstObject];
            [rightArr addObject:arr.lastObject];
        }else if (i == 2){
            [detailArr addObject:text];
        }else if (i == 3){
            [titleArr addObject:text];
            [imageArr addObjectsFromArray:itemDic[@"imgs"]];
        }
    }
    
    
    
    UIImageView *backImage = [[UIImageView alloc]initWithImage:UIImageMake(@"monitor_back")];
    backImage.frame = CGRectMake(0, 0, kScreenWidth, kScreenWidth*0.62);
    [self.view addSubview:backImage];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:UIImageMake(@"icon_arrow_left_white") forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(15, StatusBarHeightConstant, 40, 40);
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kScreenWidth*0.62 - 30, kScreenWidth, kScreenHeight - (kScreenWidth*0.62 - 30 + 60))];
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myTop = 0;
    root.myHorzMargin = 15;
    root.subviewVSpace = 15;
    [scrollView addSubview:root];
    
    
    for (int i = 0; i < titleArr.count; i++) {
        MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
        item.myHorzMargin = 0;
        item.subviewVSpace = 20;
        item.myHeight = kAutoHeight;
        item.padding = UIEdgeInsetsMake(10, 10, 10, 10);
        item.backgroundColor = COLOR_4;
        ViewCorner(item, 10);
        [root addSubview:item];
        
        MyLinearLayout *top = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        top.myHorzMargin = 0;
        top.gravity = MyGravity_Vert_Center;
        top.myHeight = 30;
        top.subviewHSpace = 10;
        [item addSubview:top];
        
        {
            UIView *line = UIView.new;
            line.mySize = CGSizeMake(4, 15);
            ViewCorner(line, 2);
            [line az_setGradientBackgroundWithColors:@[HexColor(@"#1179F2"),HexColor(@"#16BDE2")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1)];
            [top addSubview:line];
            
            UILabel *name = [UILabel createLabelText:titleArr[i] font:UIFontMake(15) textColor:COLOR1515];
            name.mySize = kAutoSize;
            name.weight = 1;
            [top addSubview:name];
            
            if(i == 0){
                UILabel *detail = [UILabel createLabelText:rightArr[i] font:UIFontMake(16) textColor:COLOR1515];
                detail.mySize = kAutoSize;
                [top addSubview:detail];
            }else if (i == 1){
                UILabel *detail = [UILabel createLabelText:rightArr[i] font:UIFontMake(18) textColor:HexColor(@"#FF921A")];
                detail.mySize = kAutoSize;
                [top addSubview:detail];
            }
        }
        
        if(i < 2){
            MyLinearLayout *content = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
            content.myHorzMargin = 0;
            content.myHeight = kAutoHeight;
            content.padding = UIEdgeInsetsMake(10,10,10,10);
            content.backgroundColor = HexColor(@"#F2F8FB");
            ViewCorner(content, 6);
            [item addSubview:content];
            
            UILabel *name = [UILabel createLabelText:detailArr[i] font:UIFontMake(15) textColor:HexColor(@"#454545")];
            name.myHorzMargin = 0;
            name.myHeight = kAutoHeight;
            [content addSubview:name];
            
        }
        else {
            MyFlowLayout *flow = [MyFlowLayout flowLayoutWithOrientation:MyOrientation_Vert arrangedCount:3];
            flow.myHorzMargin = 0;
            flow.myHeight = kAutoHeight;
            flow.subviewSpace = 15;
            flow.gravity = MyGravity_Horz_Fill;
            [item addSubview:flow];
            
            
            for (int i = 0; i < imageArr.count; i++) {
                NSDictionary *dic = imageArr[i];
                // 将base64字符串转为NSData
                NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:dic[@"img"] options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
                // 将NSData转为UIImage
                UIImage *decodedImage = [UIImage imageWithData: decodeData];

                
                MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
                item.myHorzMargin = 0;
                item.myHeight = kAutoHeight;
                item.subviewVSpace = 10;
                item.gravity = MyGravity_Horz_Center;
                [flow addSubview:item];
                
                UIImageView *imageView = [[UIImageView alloc]initWithImage:decodedImage];
                imageView.myHorzMargin = 0;
                imageView.heightSize.equalTo(imageView.widthSize);
                imageView.contentMode = UIViewContentModeScaleAspectFit;
                [item addSubview:imageView];
                
                UILabel *name = [UILabel createLabelText:dic[@"text"] font:UIFontMake(15) textColor:COLOR1515];
                name.mySize = kAutoSize;
                [item addSubview:name];
            }
            
        }
    }
    
    UIButton *submitBtn = [UIButton createButtonText:@"进入脉诊检测" font:UIFontMake(14) textColor:COLOR_4];
    submitBtn.frame = CGRectMake(15, kScreenHeight - 60, kScreenWidth - 30, 46);
    ViewCorner(submitBtn, 23);
    [submitBtn az_setGradientBackgroundWithColors:@[HexColor(@"#4BD8B1"),HexColor(@"#2CAED8")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
    [submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    
}



- (void)submitBtnClick {
    WearWatchViewController *vc = WearWatchViewController.new;
    [self showViewController:vc sender:self];
}

- (void)back {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
